package com.eduService.teacher.controller;

import com.eduService.teacher.entity.EduChapter;
import com.eduService.teacher.service.IEduChapterService;
import com.eduService.teacher.utils.R;
import com.eduService.teacher.vo.chapter.chapterAndVideo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Mr.Tang
 * @since 2022-05-01
 */
@CrossOrigin
@RestController
@RequestMapping("/educhapter")
public class EduChapterController {
    @Autowired
    IEduChapterService iEduChapterService;


    @GetMapping("getcoursechapter/{courseid}")
    public R getCourseChapter(@PathVariable("courseid") String courseid){
        List<chapterAndVideo> chapterAndVideo = iEduChapterService.getChapterAndVideo(courseid);
        return R.ok().data("chapterAndVideo",chapterAndVideo);
    }


    /*
    * 添加课程章节
    * */
    @PostMapping("saveChapter")
    public R saveChapter(@RequestBody EduChapter chapter){
        boolean saveResult = iEduChapterService.save(chapter);
        return R.ok().msg("添加章节成功！");
    }


    /*
    * 修改课程章节
    * */
    @PostMapping("updateChapter/{chapterid}")
    public R updateChapter(@RequestBody EduChapter chapter){
        boolean update = iEduChapterService.updateById(chapter);
        return R.ok().msg("修改章节成功！");
    }

    /*
    * 删除课程章节
    * */
    @DeleteMapping("delchapter/{chapterid}")
    public R delChapter(@PathVariable("chapterid") String chapterid){
        boolean exsitenceVideo = iEduChapterService.delChapter(chapterid);
        if(exsitenceVideo){
            return R.ok().msg("此章节存在小节，无法删除！").data("isdel",0);
        }else {
             iEduChapterService.removeById(chapterid);
             return R.ok().msg("删除成功！").data("isdel",1);
        }
    }

}
